package com.outfood.task;

import com.alibaba.fastjson.JSON;
import com.outfood.config.WxConfig;
import com.outfood.domain.Store;
import com.outfood.service.IStoreService;
import com.outfood.service.impl.MeiTuanOperateServiceImpl;
import com.rys.common.enums.SearchTypeEnum;
import com.rys.common.utils.DateUtils;
import com.rys.common.utils.Search;
import com.tps.open.mq.MqService;
import com.tps.open.mq.enums.QueueEnums;
import com.tps.open.mq.model.MessageBody;
import com.tps.open.weixin.dto.TemplateDTO;
import com.tps.open.weixin.dto.TemplateValue;
import lombok.extern.log4j.Log4j2;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author huisonglin
 * @Description
 * @PackageName outfood-platform
 * @Package com.outfood.task
 * @Date 2024-04-07 13:33
 * @Version 1.0
 */
@Component
@Log4j2
public class ScanStoreTask {

    @Resource
    IStoreService storeService;

    @Scheduled(cron = "0 8 */1 * * ?")
    public void scan() {
        log.info("--------------------------------------------开始检测过期门店，并修改状态------------------------------------------------------------");
        List<Store> storeList = storeService.getList(new Search().add("storeStatus", SearchTypeEnum.EqualTo, "1"));
        storeList.forEach(store -> {
            if (store.getExpireTime().getTime() < new Date().getTime()) {
                store.setStoreStatus("2");
                storeService.update(store.getId(), store, "task");
            }
        });
        log.info("--------------------------------------------开始检测过期门店，并修改状态------------------------------------------------------------");
    }


    @Scheduled(cron = "0 0 0 * * ?")
    public void remind() {
        log.info("--------------------------------------------扫描即将过期的门店提醒客户start------------------------------------------------------------");
        List<Store> storeList = storeService.getList(new Search().add("storeStatus", SearchTypeEnum.EqualTo, "1").add("onlineStatus", SearchTypeEnum.EqualTo, "ONLINE"));
        storeList.forEach(store -> {
            if (DateUtils.addDays(new Date(), 7).getTime() > store.getExpireTime().getTime()) {
                remind(store);
            }
        });
        log.info("--------------------------------------------扫描即将过期的门店提醒客户end------------------------------------------------------------");
    }

    @Resource
    MeiTuanOperateServiceImpl meiTuanOperateService;
    @Resource
    WxConfig wxConfig;
    @Reference
    MqService mqService;

    public void remind(Store store) {
        MessageBody messageBody = new MessageBody();
        List<String> openIdList = meiTuanOperateService.getOpenIdList(store.getId());
        openIdList.forEach(openId -> {
            messageBody.setHandlerBeanName("templateComsumerHandler");
            TemplateDTO templateDTO = new TemplateDTO();
            templateDTO.setTemplate_id("F1InF1QAw-8Pv46cT6yr0OsTZFRbzfo35xag4PITRi4");
            templateDTO.setTouser(openId);
            Map<String, TemplateValue> data = new HashMap<>();
            data.put("thing3", new TemplateValue(store.getStoreName().length() > 20?store.getStoreName().substring(0,17)+"...":store.getStoreName()));
            data.put("date2", new TemplateValue(DateUtils.parseDateToStr("yyyy-MM-dd", store.getExpireTime())));
            templateDTO.setPage("/pages/index/index");
            templateDTO.setData(data);
            templateDTO.setAppid(wxConfig.getWxAppid());
            templateDTO.setSecret(wxConfig.getWxSecret());
            messageBody.setBody(JSON.toJSONString(templateDTO));
            mqService.send(messageBody, QueueEnums.WEIXIN_ADMIN);
        });
    }
}
